{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-09-16T11:39:21.481544Z",
     "start_time": "2024-09-16T11:39:16.935517Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 3 4 5 6]\n"
     ]
    }
   ],
   "source": [
    "# 向量化的作用: 提高运算速度\n",
    "import  numpy as np\n",
    "\n",
    "a = np.asarray([1,3,4,5,6])\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "24925.532213613827\n",
      "向量化运行时间:0.002128124237060547\n",
      "24925.532213614053\n",
      "for-loop 运行时间:0.023821115493774414\n"
     ]
    }
   ],
   "source": [
    "#对比向量化运行时间与 for-loop 花费的时间\n",
    "\n",
    "##向量化\n",
    "import  time \n",
    "\n",
    "a = np.random.rand(100000)\n",
    "b = np.random.rand(100000)\n",
    "\n",
    "start = time.time()\n",
    "c = np.dot(a,b)\n",
    "end = time.time()\n",
    "print(c)\n",
    "print(f'向量化运行时间:{end-start}')\n",
    "\n",
    "c=0\n",
    "start = time.time()\n",
    "for i in range(100000):\n",
    "    c+=a[i]*b[i]  \n",
    "end = time.time()\n",
    "\n",
    "print(c)\n",
    "print(f'for-loop 运行时间:{end-start}')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-16T12:45:00.726454Z",
     "start_time": "2024-09-16T12:45:00.670873Z"
    }
   },
   "id": "3984c73f40924d51",
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# python 中的广播 \n",
    "\n",
    "## 当一个(m,n)的矩阵  加减乘除 一个 (1,n)的行向量时,行向量会自动被扩展为(m,n)的矩阵\n",
    "    ### 例如 (1,2,3)  和 (100,200,300)的行向量 ,行向量会被扩展为[[100,200,300],[100,200,300]]的 m*n 矩阵\n",
    "    ###     (4,5,6)\n",
    "    \n",
    "##当一个(m,n)的矩阵  加减乘除 一个 (m,1)的列向量时,行向量会自动被扩展为(m,n)的矩阵\n",
    "    ### 例如 [[1,2,3],[4,5,6]] 和 [[100],[200]]的列向量, 列向量会被扩展为[[100,100,100].[200,200,200]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-16T11:48:07.498380Z",
     "start_time": "2024-09-16T11:48:07.495961Z"
    }
   },
   "id": "60f938cfa4d88f3d",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------数组--------\n",
      "a_array:秩为 1 个数组:[-0.91920986 -0.39546711  0.04821604  0.41317696 -0.25510151]\n",
      "(5,)\n",
      "1.2394577681700414\n",
      "------矩阵--------\n"
     ]
    },
    {
     "ename": "AssertionError",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mAssertionError\u001B[0m                            Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[14], line 10\u001B[0m\n\u001B[1;32m      8\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m------矩阵--------\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[1;32m      9\u001B[0m a_vector \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39mrandom\u001B[38;5;241m.\u001B[39mrandn(\u001B[38;5;241m5\u001B[39m,\u001B[38;5;241m1\u001B[39m) \u001B[38;5;66;03m#得到五行一列的列向量\u001B[39;00m\n\u001B[0;32m---> 10\u001B[0m \u001B[38;5;28;01massert\u001B[39;00m (a_vector\u001B[38;5;241m.\u001B[39mshape\u001B[38;5;241m==\u001B[39m(\u001B[38;5;241m5\u001B[39m,\u001B[38;5;241m2\u001B[39m))\n\u001B[1;32m     11\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124ma_vector矩阵:\u001B[39m\u001B[38;5;132;01m{\u001B[39;00ma_vector\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m'\u001B[39m)\n\u001B[1;32m     12\u001B[0m \u001B[38;5;28mprint\u001B[39m(a_vector\u001B[38;5;241m.\u001B[39mshape)\n",
      "\u001B[0;31mAssertionError\u001B[0m: "
     ]
    }
   ],
   "source": [
    "# 不要使用 randn(5),得到的是秩为 1 的数组,并不是一个矩阵\n",
    "print('------数组--------')\n",
    "a_array= np.random.randn(5)\n",
    "print(f'a_array:秩为 1 个数组:{a_array}')\n",
    "print(a_array.shape)  # 得到的是一个 秩为 1 个数组,而不是矩阵,这导致和转置矩阵内积出来的是一个数字,而不是矩阵\n",
    "print(np.dot(a_array,a_array.T))\n",
    "\n",
    "print('------矩阵--------')\n",
    "a_vector = np.random.randn(5,1) #得到五行一列的列向量\n",
    "# assert (a_vector.shape==(5,1))\n",
    "print(f'a_vector矩阵:{a_vector}')\n",
    "print(a_vector.shape)\n",
    "print(np.dot(a_vector,a_vector.T))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-16T12:55:35.898648Z",
     "start_time": "2024-09-16T12:55:35.691712Z"
    }
   },
   "id": "cd7d65f83aac15d6",
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "abd39c7ab7aaad79"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
